﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Threading;
using System.IO;
using System.Globalization;

namespace CompanyManagement
{
    class LogInfo
    {
        public String mess;
        public String name;
        public String functionName;
        public String paymentID;

        public LogInfo(String mess, String name, String functionName, String paymentID)
        {
            this.mess = mess;
            this.name = name;
            this.functionName = functionName;
            this.paymentID = paymentID;
        }
    }
    public class Tools
    {
        static string pathLog = @"C:\Log\Companies\";
        static object syncObj = new object();

        public static void Logger(String mess, String name, String functionName, String paymentID = "0")
        {
            LogInfo logInfo = new LogInfo(mess, name, functionName, paymentID);
            new Thread(new ParameterizedThreadStart(writeLog)).Start(logInfo);
        }

        static void writeLog(Object objLog)
        {
            LogInfo logInfo = (LogInfo)objLog;
            lock (syncObj)
            {
                try
                {
                    Directory.CreateDirectory(pathLog + DateTime.Now.ToString("yyyy-MM-dd"));
                    StreamWriter sw = new StreamWriter(pathLog +
                                                        DateTime.Now.ToString("yyyy-MM-dd") +
                                                        "\\" + DateTime.Now.ToString("MM-dd-HH") +
                                                        logInfo.name + ".txt", true);

                    sw.WriteLine(DateTime.Now.ToString("HH-mm-ss fff") + " : [" + logInfo.functionName + "] :: " + logInfo.mess);

                    sw.Flush();
                    sw.Close();
                }
                catch
                {
                }
            }
        }

        // Remove Accent for filtering Radgrid
        public static string RemoveDiacritics(string s)
        {
            string stFormD = s.Normalize(System.Text.NormalizationForm.FormD);
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            string str = "";
            for (int i = 0; i <= stFormD.Length - 1; i++)
            {
                UnicodeCategory uc = CharUnicodeInfo.GetUnicodeCategory(stFormD[i]);
                if (uc == UnicodeCategory.NonSpacingMark == false)
                {
                    if (stFormD[i] == 'đ')
                        str = "d";
                    else if (stFormD[i] == 'Đ')
                        str = "D";
                    else if (stFormD[i] == '\r' | stFormD[i] == '\n')
                        str = "";
                    else
                        str = stFormD[i].ToString();
                    sb.Append(str);
                }
            }
            return sb.ToString();
        }

        public static string GeneratorPassword(byte Length)
        {
            char[] Chars = new char[] {
                'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
                'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
                '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
            };
            string String = string.Empty;
            Random Random = new Random();

            for (byte a = 0; a < Length; a++)
            {
                String += Chars[Random.Next(0, 61)];
            };

            return (String);
        }

        public static string RemoveTokens(string s)
        {
            String regex = "\\s*\\b\\s*";
            return s.Replace(regex, "");
        }
    }
}